package org.pet.service.impl;

import org.pet.entity.PetSubVariety;
import org.pet.entity.PetVariety;
import org.pet.mapper.PetVarietyMapper;
import org.pet.service.PetVarietyService;
import org.pet.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetVarietyServiceImpl implements PetVarietyService {

    @Autowired
    private PetVarietyMapper petVarietyMapper;

    //获得一级目录
    @Override
    public Result list() {
        List<PetVariety> list = petVarietyMapper.list();
        if (list != null) {//list不为null则返回list，为空也算成功
            return Result.success(list);
        }
        else {
            return Result.error("操作失败");
        }
    }
    //获得二级目录
    @Override
    public Result subList(Integer id) {
        List<PetSubVariety> list = petVarietyMapper.subList(id);
        if (list != null) {//list不为null则返回list，为空也算成功
            return Result.success(list);
        }
        else {
            return Result.error("操作失败");
        }
    }

    @Override
    public Integer getVarietyId(String variety) {
        return petVarietyMapper.getVarietyId(variety);
    }
    @Override
    public String getVariety(String category){
        return  petVarietyMapper.getVariety(category);
    }
    @Override
    public Result insert(PetVariety petVariety){
        return petVarietyMapper.add(petVariety)==0 ? Result.error("操作失败") : Result.success();
    }

    @Override
    public Result subInsert(String variety, String category, String pic){
        //先判断variety是否存在
        Integer id = getVarietyId(variety);
        if( id == null ){
            PetVariety petVariety = new PetVariety();
            petVariety.setVariety(variety);
            petVarietyMapper.add(petVariety);
            id = getVarietyId(variety);
        }
        PetSubVariety petSubVariety = new PetSubVariety();
        if(id == null){
            return  Result.error("操作失败");
        }
        petSubVariety.setCategory(category);
        petSubVariety.setPic(pic);
        petSubVariety.setVariety(id);
        return petVarietyMapper.subAdd(petSubVariety)==0 ? Result.error("操作失败") : Result.success();
    }

    @Override
    public Result delete(Integer id) {
        //先删除子节点
        List<PetSubVariety> list = petVarietyMapper.subList(id);
        if(list != null && !list.isEmpty()){//节点之下是否还有子节点，有就一起删除
            if(petVarietyMapper.subDeleteAll(id) == 0) return Result.error("操作失败");
        }
        return petVarietyMapper.delete(id)==0 ? Result.error("操作失败") : Result.success();
    }

    @Override
    public Result subDelete(Integer id) {
        return petVarietyMapper.subDelete(id)==0 ? Result.error("操作失败") : Result.success();
    }

    @Override
    public Result update(PetVariety petVariety) {
        return petVarietyMapper.update(petVariety)==0 ? Result.error("操作失败") : Result.success();
    }

    @Override
    public Result subUpdate(Integer id, String variety, String category, String pic) {
        Integer varietyId = getVarietyId(variety);
        if (varietyId == null){
            return Result.error("宠物种类不存在！");
        }
        PetSubVariety petSubVariety = new PetSubVariety();
        petSubVariety.setId(id);
        petSubVariety.setVariety(varietyId);
        petSubVariety.setCategory(category);
        petSubVariety.setPic(pic);
        return petVarietyMapper.subUpdate(petSubVariety)==0 ? Result.error("操作失败") : Result.success();
    }

    @Override
    public Result searchList(String search) {
        List<PetSubVariety> list = petVarietyMapper.searchList(search);
        if (list != null) {//list不为null则返回list，为空也算成功
            return Result.success(list);
        }
        else {
            return Result.error("操作失败");
        }
    }

    @Override
    public Result searchNotExit() {
        List<PetSubVariety> list = petVarietyMapper.searchNotExit();
        if (list != null) {//list不为null则返回list，为空也算成功
            return Result.success(list);
        }
        else {
            return Result.error("操作失败");
        }
    }
}
